/* ============================================================================ */
/*                         IMPORT DATA AND SET GLOBALS                          */
/* ============================================================================ */

clear all // Clear all stored data and variables

/***************************************
*           UNMATCHED SAMPLE           *
***************************************/

/* Import firm-level loan data (SIREN - CIB dataset) */
use "$output\SCR_CIB_3_months_orbis_scrapped.dta", clear

/* Keep observations within the time range 2014-2019 */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* ============================================================================ */
/*            Compute Subsequent Loan Variable (Bank Loan between [0,5])        */
/* ============================================================================ */

/* Keep only loans that are not outside loans */
keep if newoutside == 0 

/* Select firms at t=-1 and t=5 */
keep if deltamonths == -1 | deltamonths == 5
sort siren mdate cib

/* Generate industry groups */
egen nesc = group(industry)

/* Collapse data to compute total bank loan amounts and average assets */
collapse (sum) stloan ltloan bm bi oc (mean) assets_filled, by(siren p2p deltamonths nesc year)

xtset siren deltamonths // Set panel structure for time-series operations

/* Compute new loan amount (difference between t=5 and t=-1) */
gen loan = stloan + ltloan + bm + bi + oc
gen loang = loan - L6.loan

/* Keep only observations at t=5 -> change in loans in the six months after the outside loan */
keep if deltamonths == 5

/* Keep relevant variables */
keep siren p2p loang nesc year

/* Save the processed dataset */
save "$output\subsequent_loans.dta", replace	

/***************************************
*           OUTSIDE LOAN           *
***************************************/

/* Import firm-level loan data again */
use "$output\SCR_CIB_3_months_orbis_scrapped.dta", clear

/* Keep observations within the time range 2014-2019 */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Collapse data to compute mean outside loan amounts at firm level */
collapse (mean) outsideloan, by(siren) 

/* ============================================================================ */
/*             Merge Outside Loan Data with Subsequent Loan Data                */
/* ============================================================================ */

merge 1:1 siren using "$output\subsequent_loans.dta"
keep if _merge == 3  // Keep only matched observations

/* ============================================================================ */
/*               Winsorize Outliers at 1% and 99% Percentiles                   */
/* ============================================================================ */

foreach x of varlist loang outsideloan {
    winsor2 `x', suffix(_trim) cut(1 99) trim
}

/* ============================================================================ */
/*               REGRESSION ANALYSIS AND BIN SCATTER PLOTS                      */
/* ============================================================================ */

/* Start logging */
capture log close
log using "$logs\binscatter_outsideloan_subsequent_loan.log", replace

/* ============================================================================ */
/*                Generate Binned Scatter Plot (Figure)                         */
/* ============================================================================ */

binscatter loang_trim outsideloan_trim if outsideloan_trim <= 400, ///
legend(order(2 "FinTech borrowers" 1 "Bank borrowers") region(lstyle(none))) ///
by(p2p) n(20) reportreg ///
mcolor(gs10%40 emerald%60) msymbol(T O) lcolor(gs10 emerald ) ///
ytitle("Size of bank credit after origination (k)") ///
xtitle("Size of outside loan (k)") ///
text(155 280 "y=15.12 + 0.35x" "               (***)", place(e) color(emerald)) ///
text(20 280 "y=-3.21 + 0.14x" "             (***)", place(e) color(gs10)) ///
plotregion(fcolor(white) lcolor(white)) graphregion(color(white))

/* Export the graph */
graph export "$figures\binscatter_400_cap.pdf", replace
graph save "Graph" "$figures\binscatter_400_cap.gph", replace

/* Close log file */
log close
